import {reqAddressInfo, reqShopInfo, reqSubmitOrder} from '@/api'

const state = {
  address:[],
  shopInfo:{},
  payId:''
}
const mutations = {
  GETADDRESS(state,address){
    state.address = address
  },
  GETSHOPINFO(state,shopInfo){
    state.shopInfo = shopInfo
  },
  SUBMITORDER(state,payId){
    state.payId = payId;
}
}
const actions = {
  // 获取用户地址信息
  async getAddressInfo({commit}){
    let res = await reqAddressInfo()
    // console.log(res.data)
    if(res.data.code===200){
      commit('GETADDRESS',res.data.data)
    }
  },
  // 获取商品清单数据
  async getShopInfo({commit}){
    let res = await reqShopInfo()
    // console.log(res)
    if(res.data.code===200){
      commit('GETSHOPINFO',res.data.data)
    }
  },
  //提交订单:tradeNO 交易编码   data:付款人信息
  async submitOrder({commit}, { tradeNo, data }) {
    //提交订单的时候：返回一个很重要数据->订单ID【这笔交易唯一标识符:付款人、收款人】
    let res = await reqSubmitOrder(tradeNo, data);
    console.log(res.data)
    if (res.data.code == 200) {
        commit('SUBMITORDER', res.data.data);
        return 'ok';
    } else {
        return Promise.reject(new Error(res.data.message));
    }
}
}
const getters = {}

export default {
  state,
  mutations,
  actions,
  getters
}